home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.05 May 96 / AppleScript Optimization < prev    next >
Encoding:
Text File  |  1996-04-18  |  2.7 KB  |  112 lines  |  [TEXT/R*ch]

  1. Split up your scripting projects.  Use the load script command to load
  2. pre-compiled portions of your script project.  If you’re using Script Editor,
  3. Script Wizard, FaceSpan or Scripter, it’s probably best to store your
  4. pre-compiled scripts in a known location so that you can use simple Load Script
  5. statements:
  6.  
  7. property lib1 : load script alias "My HD:Script Libraries:Lib1"
  8.  
  9.     Alternatively, you can store your libraries in the same folder as your
  10. script editor, and load them with the following command:
  11.  
  12. property lib1 : load script "Lib1"
  13.  
  14.     If you’re using Script Debugger, you can use its path to me handling to
  15. store your libraries in a folder with your script:
  16.  
  17. property projectPath :¬
  18.         «property ctnr» of item (path to me) of ¬
  19.         application "Finder" as string
  20. property lib1 : load script (projectPath & "Lib1")
  21.     Once you’ve loaded your library, you can access properties and call handlers
  22. stored within it like this:
  23.  
  24. propertyName of lib1
  25.  
  26. handlerName(p1, p2, p3) of lib1
  27.  
  28. ***
  29.  
  30.  
  31.  
  32.  
  33.     The slow way:
  34.  
  35. tell application "Finder"
  36.         repeat with aItem in (items of first window)
  37.                 if name of aItem ends with ".temp" then
  38.                         delete aItem
  39.                 end
  40.         end
  41. end
  42.  
  43.     The fast way:
  44.  
  45. tell application "Finder"
  46.         delete (items whose name ends with ".temp") ¬
  47.                 of first window
  48. end
  49.  
  50.     The next error people commonly make is to repeatedly ask for the value of
  51. properties.  For example, you might do the following:
  52.  
  53. tell application "Finder"
  54.         repeat with aItem in items of window 1
  55.                 if name of aItem ends with ".c" or ¬
  56.                         name of aItem ends with ".h" then
  57.                         -- do something with .c and .h files
  58.                 end
  59.         end
  60. end
  61.  
  62.     This script fragment gets the value of the application’s name property
  63. twice.  A faster method is to get the name property once:
  64.  
  65. tell application "Finder"
  66.         repeat with aItem in items of window 1
  67.                 set theFileName to name of aItem
  68.                 if theFileName ends with ".c" or ¬
  69.                         theFileName ends with ".h" then
  70.                         -- do something with .c and .h files
  71.                 end
  72.         end
  73. end
  74.  
  75.  
  76. ***
  77.  
  78.  
  79. So, in the following example, the current date command (which is a scripting
  80. addition command) is executed within the Scriptable Text Editor’s process rather
  81. than the applet:
  82.  
  83. tell application "Scriptable Text Editor"
  84.                 make new line at end of first window ¬
  85.                                 with data (current date as string)
  86. end
  87.  
  88.     An improvement in speed would result if you did it this way:
  89.  
  90. set theData to current date as string
  91. tell application "Scriptable Text Editor"
  92.                 make new line at end of first window ¬
  93.                                 with data theData
  94. end
  95.  
  96.     If you are deep within a tell block, you could deal with the problem this
  97. way:
  98.  
  99. tell application "Scriptable Text Editor"
  100.                 ...
  101.                 make new paragraph at end of first window with ¬
  102.                                 data ((current date) of me as string)
  103.                 ...
  104. end
  105.  
  106.  
  107.  
  108. ***
  109.  
  110.  
  111.  
  112.